<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册页面</title>
    <!-- 引入Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <!-- 引入 Bootstrap Icons -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css">
    <!-- 引入 layui.css -->
    <link href="//unpkg.com/layui@2.9.14/dist/css/layui.css" rel="stylesheet">
    <!-- 自定义样式 -->
    <style>
        .register-container {
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 5px;
        }

        .announcement-container {
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 5px;
            height: 100%;
            overflow: hidden; /* 隐藏滚动条 */
            position: relative; /* 使子元素可以绝对定位 */
        }

        .announcement-wrapper {
            height: 300px; /* 定义显示区域的高度 */
            overflow: hidden;
            position: relative;
        }

        .announcement-list {
            position: absolute;
            width: 100%;
            overflow: hidden;
            box-sizing: border-box;
            animation: scroll-vertical 10s linear infinite;
        }

        .announcement-item {
            display: flex; /* 使用flex布局 */
            align-items: center; /* 垂直居中 */
        }

        .announcement-item img {
            height: 30px;
            width: 30px;
            margin-right: 10px;
            border-radius: 50%;
        }

        @keyframes scroll-vertical {
            0% {
                transform: translateY(100%);
            }
            100% {
                transform: translateY(-100%);
            }
        }

        [v-cloak] {
            display: none;
        }

        .marquee {
            width: 100%;
            white-space: nowrap;
            overflow: hidden;
            box-sizing: border-box;
            animation: marquee 10s linear infinite;
            background: #f8d7da;
            color: #721c24;
            padding: 10px;
            margin-bottom: 20px;
        }

        @keyframes marquee {
            0% {
                transform: translateX(100%);
            }
            100% {
                transform: translateX(-100%);
            }
        }

    </style>
</head>
<body>
<div id="app" v-cloak>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <a class="navbar-brand" href="#"><i class="bi bi-book"></i> 罗邓图书商城借阅系统</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="切换导航">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav ml-auto">
                <li class="nav-item">
                    <a class="nav-link" href="#"><i class="bi bi-info-circle"></i> 关于我们</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="login.html"><i class="bi bi-box-arrow-in-right"></i> 登录</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#"><i class="bi bi-shield-lock"></i> 隐私政策</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#"><i class="bi bi-question-circle"></i> 忘记密码</a>
                </li>
            </ul>
        </div>
    </nav>
    <!--导航栏end-->

    <!-- 滚动提示��� -->
    <div class="marquee"><i class="bi bi-megaphone"></i> 欢迎来到注册页面，有问题请联系我们的客服! qq:3066992141</div>

    <div class="container">
        <div class="row">
            <!-- 公告展示区 -->
            <div class="col-md-4">
                <div id="announcement" class="announcement-container">
                    <h4><i class="bi bi-people-fill"></i> 最近注册用户</h4>
                    <div class="announcement-wrapper">
                        <ul class="announcement-list list-group" style="margin-top: 20px;">
                            <li class="announcement-item list-group-item" v-for="(user,index) in recentUsers"
                                :key="index">
                                <img :src="user.profile" alt="avatar">
                                <div>
                                    <strong>用户名：</strong> {{ user.username }}<br>
                                    <strong>姓名：</strong> {{ user.name }}<br>
                                    <strong>创建时间：</strong> {{ formatDate(user.create_time) }}
                                </div>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>

            <!-- 注册表单 -->
            <div class="col-md-8">
                <div class="register-container">
                    <h2 class="text-center"><i class="bi bi-person-plus"></i> 注册</h2>
                    <!--      <form>-->
                    <div class="form-group">
                        <label for="name"><i class="bi bi-person"></i> 姓名</label>
                        <input type="text" class="form-control" id="name" v-model="formData.name" required>
                    </div>
                    <div class="form-group">
                        <label for="username"><i class="bi bi-person-circle"></i> 用户名</label>
                        <input type="text" class="form-control" id="username" v-model="formData.username" required>
                    </div>
                    <div class="form-group">
                        <label><i class="bi bi-gender-ambiguous"></i> 性别</label>
                        <div>
                            <label class="radio-inline"><input type="radio" v-model="formData.sex" value="男"> 男
                            </label>
                            <label class="radio-inline"><input type="radio" v-model="formData.sex" value="女"> 女
                            </label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="password"><i class="bi bi-lock"></i> 密码</label>
                        <input type="password" class="form-control" id="password" v-model="formData.password"
                               required>
                    </div>
                    <div class="form-group">
                        <label for="email"><i class="bi bi-envelope"></i> 邮箱</label>
                        <input type="email" class="form-control" id="email" v-model="formData.email" required>
                    </div>
                    <button class="btn btn-primary btn-block" @click="getcode"><i class="bi bi-check-circle"></i> 注册</button>
                    <!--         </form>-->
                </div>
            </div>
        </div>
    </div>

    <!-- 邮箱验证码弹窗 -->
    <div id="email-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="email-modal-label"
         aria-hidden="true" data-bs-backdrop="static">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="email-modal-label"><i class="bi bi-envelope-open"></i> 请输入邮箱验证码</h5>
                    <button type="button" @click="closeModal" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <input type="text" class="form-control" id="emailCode" v-model="verificationCode"
                           placeholder="验证码">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal" @click="closeModal"><i class="bi bi-x-circle"></i> 取消
                    </button>
                    <button type="button" class="btn btn-primary" @click="submitRegistration"><i class="bi bi-check-circle"></i> 确认</button>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 引入Vue.js -->
<!--<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>-->
<script src="js/vue@2.js"></script>
<!-- 引入Axios -->
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<!-- 引入jQuery -->
<!--<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>-->
<script src="js/jquery.min.js"></script>
<!-- Bootstrap JS 和依赖 -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<!-- 引入 layui.js -->
<script src="//unpkg.com/layui@2.9.14/dist/layui.js"></script>
<script src="js/pulblicfuntion.js"></script>
<script src="js/data.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            // 表单数据
            formData: {
                name: '',
                username: '',
                sex: '',
                password: '',
                email: ''
            },
            // 验证码
            verificationCode: '',
            // 最近注册用户
            recentUsers: []
        },
        created() {
            layer.msg('欢迎来到注册页面!', {icon: 1, time: 1000});
            // 获取最近注册用户列表
            axios.get(urlluodeng + '/user/getSomeUser6').then(response => {
                if (response.data.result) {
                    this.recentUsers = response.data.data;
                    /* for(var i=0;i<10;i++){
                         this.recentUsers= this.recentUsers.concat(response.data.data);
                     }*/
                } else {
                    layer.msg('获取用户列表失败,请联系管理员', {icon: 2, time: 1000});
                }
            }).catch(error => {
                console.log(error);
                layer.msg('获取最近注册用户列表失败', {icon: 2, time: 1000});
            });
        },
        methods: {
            closeModal() {
                // 显示验证码弹窗
                $('#email-modal').modal('hide');
            },
            formatDate(isoString) {
                const date = new Date(isoString);
                const year = date.getFullYear();
                const month = String(date.getMonth() + 1).padStart(2, '0'); // getMonth() 返回值为 0-11
                const day = String(date.getDate()).padStart(2, '0');
                return `${year}-${month}-${day}`;
            },
            // 验证表单数据并显示验证码弹窗
            validateForm() {

            },
            getcode() {
                if (!this.formData.email || !this.formData.username || !this.formData.password || !this.formData.name || !this.formData.sex) {
                    layer.msg('必填信息不能为空!', {icon: 2, time: 1000});
                    return;
                } else {
                    let formdata = new FormData();
                    console.log('要注册的用户信息:');
                    formdata.append('email', this.formData.email);
                    formdata.append('username', this.formData.username);
                    formdata.append('password', this.formData.password);
                    formdata.append('name', this.formData.name);
                    formdata.append('sex', this.formData.sex);
                    console.log(formdata);
                    // 发送验证码请求同时把表单数据一起发送
                    axios.post(urlluodeng + '/user/sendEmailCode', formdata).then(response => {
                        if (response.data.result) {
                            layer.msg(response.data.message, {icon: 1, time: 1000});
                            setTimeout(() => {
                                // 显示验证码弹窗
                                $('#email-modal').modal('show');
                            }, 1500);
                        } else {
                            layer.msg('验证码发送失败，请重试', {icon: 2, time: 1000});
                        }
                    }).catch(error => {
                        console.log(error);
                        layer.msg('验证码发送失败，请重试', {icon: 2, time: 1000});
                    });
                }
            },
            submitRegistration() {
                if (this.verificationCode === '') {
                    layer.msg('验证码不能为空!', {icon: 2, time: 1000});
                    return;
                } else {
                    let submitData = new FormData();
                    console.log('要注册的用户信息:');
                    submitData.append('code', this.verificationCode);
                    axios.post(urlluodeng + '/user/register', submitData).then(response => {
                        if (response.data.result) {
                            layer.msg(response.data.message, {icon: 1, time: 1000});
                            // 关闭验证码弹窗
                            $('#email-modal').modal('hide');
                            this.formData = {
                                name: '',
                                username: '',
                                sex: '',
                                password: '',
                                email: ''
                            };
                            /*  // 刷新页面
                              window.location.reload();*/
                        } else {
                            layer.msg(response.data.message, {icon: 2, time: 1000});
                        }
                    }).catch(error => {
                        console.log(error);
                        layer.msg('注册失败，请检查网络连接', {icon: 2, time: 1000});
                    });
                }
            }

            // 提交注册数据
            /*   submitRegistration () {
                   // 关闭验证码弹窗
                  /!* $('#email-modal').modal('hide');*!/

                   // 组合表单数据与验证码
                   const dataToSubmit = {
                       ...this.formData,
                       verificationCode: this.verificationCode
                   };

                   // 模拟 POST 请求
                   setTimeout(() => {
                       alert('注册成功!');
                       console.log(dataToSubmit);
                       // 更新最近注册用户列表
                       this.recentUsers.push({
                           avatar: 'your-avatar-url.jpg',
                           username: this.formData.username,
                           name: this.formData.name,
                           create_time: new Date().toISOString()
                       });
                       // 清空表单数据
                       this.formData = {
                           name: '',
                           username: '',
                           sex: '',
                           password: '',
                           email: ''
                       };
                       this.verificationCode = '';
                   }, 1000);
               }*/
        }
    });
</script>
</body>
</html>
